package com.itheima.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.text.normalizer.UBiDiProps;

import java.util.ArrayList;
import java.util.List;

/**
 * reggie-com.itheima.Controller
 * Administrator
 * 2023-03-13
 * 16:24
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    //查看购物车
    //@GetMapping("/list")
    //public R list(){
    //    return R.success(new ArrayList<>());
    //}


    @PostMapping("/add")
    public R save(@RequestBody ShoppingCart shoppingCart){
        Long currentId = BaseContext.getCurrentId();
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,currentId);
        if (dishId!=null){
            lqw.eq(ShoppingCart::getDishId,dishId);
        }else {
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(lqw);
        // 第一次点的时候是新增 ，后面在点击的时候是修改
        if (one==null){
            // 如果没有，则是新增
            // 设置 number=1
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(currentId);
            shoppingCartService.save(shoppingCart);
        }else {
            Integer number = one.getNumber();
            number=number+1;
            one.setNumber(number);
            shoppingCartService.updateById(one);
        }
        return  R.success("添加成功");
    }

    @GetMapping("/list")
    public R selectShoppingCart(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,currentId);
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        if (list.size()!=0){
            return  R.success(list);
        }
        return R.error("购物车为空");

    }

    @PostMapping("/sub")
    public R subCart(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        Long currentId = BaseContext.getCurrentId();
        lqw.eq(ShoppingCart::getUserId,currentId);
        Long dishId = shoppingCart.getDishId();
        if (dishId!=null){
            lqw.eq(ShoppingCart::getDishId,dishId);
            ShoppingCart one = shoppingCartService.getOne(lqw);
            Integer number = one.getNumber();
            Long id = one.getId();
            if (number==1){
                shoppingCartService.removeById(id);
            }else {
                number=number-1;
                one.setNumber(number);
                shoppingCartService.updateById(one);
            }

            } else {
            Long setmealId = shoppingCart.getSetmealId();
            lqw.eq(ShoppingCart::getSetmealId,setmealId);
            ShoppingCart one = shoppingCartService.getOne(lqw);
            Integer number = one.getNumber();
            Long id = one.getId();
            if (number==1){
                shoppingCartService.removeById(id);
            }else {
                number=number-1;
                one.setNumber(number);
                shoppingCartService.updateById(one);
            }
        }
        LambdaQueryWrapper<ShoppingCart> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(ShoppingCart::getId,currentId);
        List<ShoppingCart> list = shoppingCartService.list(lqw1);
        return  R.success(list);
    }

    @DeleteMapping("/clean")
    public R cleanCart(){
        Long  userId= BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        boolean remove = shoppingCartService.remove(lqw);
        if (remove){
            return R.success("清空购物车成功");
        }
        return R.error("清口购物车失败");
    }

}
